import request from "../../utils/request"
Page({
  /**
   * 页面的初始数据
   */
  data: {
    typeList: [{
      id: 0,
      title: "全部"
    }, {
      id: 1,
      title: "待支付"
    }, {
      id: 2,
      title: "已支付"
    }, {
      id: 3,
      title: " 已发货"
    }, {
      id: 4,
      title: "已完成"
    }, {
      id: 5,
      title: "已取消"
    }],
    list: [],
    page: 1,
    total: 0,
    curretnType: 0
  },
  tabChange(e) {
    console.log(e.detail.name); // 寻中项目的id值
    this.data.page = 1;
    this.data.list = [];
    this.data.curretnType = e.detail.name;
    this.getList(e.detail.name)
  },
  async getList(type = 0) {
    const userId = wx.getStorageSync('userInfo').id
    const res = await request.get(`/orders/page?status=${type}&userId=${userId}&pageNum=${this.data.page}`);
    console.log(res);
    this.setData({
      list: this.data.list.concat(res.data.data),
      total: res.data.total
    })
  },
  async wxpay(e) {
    const {
      id
    } = e.currentTarget.dataset;
    const payres = await request.post('/pay/create', {
      orderId: id,
      openId: wx.getStorageSync('userInfo').openid
    })
    wx.requestPayment({
      ...payres.data.data,
      signType: "MD5",
      success: res => {
        console.log(res);
        this.data.page = 1;
        this.data.list = [];
        this.getList(this.data.curretnType);
        wx.showToast({
          title: '支付成功',
        })
      },
      fail: err => {
        console.log(err);
        wx.showToast({
          title: '支付失败',
        })
      }
    })
    console.log(payres);
  },
  cancelOrder(e) {
    console.log(e);
    const {
      orderno
    } = e.currentTarget.dataset;
    const userId = wx.getStorageSync('userInfo').id;
    wx.showModal({
      title: '提示',
      content: '此操作将取消订单,是否继续？',
      complete: async (res) => {
        if (res.confirm) {
          const res = await request.post(`/orders/cancel`, {
            orderNo: orderno,
            userId
          });
          this.data.page = 1;
          this.data.list = [];
          this.getList(this.data.curretnType);
          console.log(res);
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      curretnType: +options.type || 0,
    })
    console.log(options);
    this.getList(+options.type || 0);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})